/*
 * DBHelper.cpp
 *
 *  Created on: Jan 23, 2012
 *      Author: hduser
 */

#include <stdlib.h>
#include <iostream>
#include "DBHelper.h"
#include "prepared_statement.h"

using namespace std;
using namespace sql;

namespace database {

const char* HOST = "localhost";
const char* USER = "root";
const char* PASS = "root";
const char* DB = "dCDN";

DBHelper::DBHelper() {

	try {
		sql::Driver * driver = sql::mysql::get_driver_instance();
		con = driver->connect(HOST, USER, PASS);
		con->setSchema(DB);
	} catch (sql::SQLException &e) {
		/*
		The MySQL Connector/C++ throws three different exceptions:

		- sql::MethodNotImplementedException (derived from sql::SQLException)
		- sql::InvalidArgumentException (derived from sql::SQLException)
		- sql::SQLException (derived from std::runtime_error)
		*/
		cerr << "# ERR: SQLException in " << __FILE__;
		cerr << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
		/* Use what() (derived from std::runtime_error) to fetch the error message */
		cerr << "# ERR: " << e.what();
		cerr << " (MySQL error code: " << e.getErrorCode();
		cerr << ", SQLState: " << e.getSQLState() << " )" << endl;
	}
}

DBHelper::~DBHelper() {

	try {
		if (con != NULL) {
			con->close();
			delete con;
		}
	} catch (sql::SQLException &e) {
		/*
		The MySQL Connector/C++ throws three different exceptions:

		- sql::MethodNotImplementedException (derived from sql::SQLException)
		- sql::InvalidArgumentException (derived from sql::SQLException)
		- sql::SQLException (derived from std::runtime_error)
		*/
		cerr << "# ERR: SQLException in " << __FILE__;
		cerr << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
		/* Use what() (derived from std::runtime_error) to fetch the error message */
		cerr << "# ERR: " << e.what();
		cerr << " (MySQL error code: " << e.getErrorCode();
		cerr << ", SQLState: " << e.getSQLState() << " )" << endl;
	}
}

int DBHelper::logErrorToDB(char* host, char* clientip, char* serverip, char* location, char* errormsg, char* note) {

	try {

		//Statement* stmt = con->createStatement();
		//stmt->execute("");

		cout<< "in logErrorToDB - " << errormsg << endl;

		sql::PreparedStatement* pstmt = con->prepareStatement("CALL spiErrorLog(?,?,?,?,?,?)");

		if (host != NULL)
			pstmt->setString(1, host);
		else
			pstmt->setString(1, "null");

		if (clientip != NULL)
			pstmt->setString(2, clientip);
		else
			pstmt->setString(2, "null");

		if (serverip != NULL)
			pstmt->setString(3, serverip);
		else
			pstmt->setString(3, "null");

		if (location != NULL)
			pstmt->setString(4, location);
		else
			pstmt->setString(4, "null");

		if (errormsg!= NULL)
			pstmt->setString(5, errormsg);
		else
			pstmt->setString(5, "null");

		if (note != NULL)
			pstmt->setString(6, note);
		else
			pstmt->setString(6, "null");

		pstmt->execute();

		delete pstmt;


		return EXIT_SUCCESS;

	} catch (sql::SQLException &e) {
		/*
		The MySQL Connector/C++ throws three different exceptions:

		- sql::MethodNotImplementedException (derived from sql::SQLException)
		- sql::InvalidArgumentException (derived from sql::SQLException)
		- sql::SQLException (derived from std::runtime_error)
		*/
		cerr << "# ERR: SQLException in " << __FILE__;
		cerr << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
		/* Use what() (derived from std::runtime_error) to fetch the error message */
		cerr << "# ERR: " << e.what();
		cerr << " (MySQL error code: " << e.getErrorCode();
		cerr << ", SQLState: " << e.getSQLState() << " )" << endl;

		return EXIT_FAILURE;
	}
}

}
